//
//  SourcesOrderVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/30.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "SourcesOrderVC.h"
#import "SourcesOrderListVC.h"
#import "SourceViewController.h"
#import "StrongReminderView.h"
#import "OrderListSearchVC.h"
#import <WZLBadge/WZLBadgeImport.h>

@interface SourcesOrderVC ()
@property (nonatomic,strong) NSMutableArray *arrBadgeNums;
@property (nonatomic,strong) CJButton *btnSearch;
@property (nonatomic,strong) CJButton *btnOrderList;
@property (copy, nonatomic) NSDictionary *dicSearch;
@end

@implementation SourcesOrderVC
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor cjMainTextColor];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    if ([CJControl isLogin]) {
        if ([CJControl getIdCardDateHasDue] && [CJControl getStrongReminderShow]) {
            [[[StrongReminderView shared]initWithView] show];
        }else{
            [[[StrongReminderView shared]initWithView] hidden];
        }
    }
    
}

- (void)viewDidLoad {
    // Do any additional setup after loading the view.
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont cjTitleFont18];
    titleLabel.textColor = [UIColor cjMainTextColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"运单列表";
    
    UIImage *img = [[UIImage imageNamed:@"zcj_arrow_back"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleDone target:self action:@selector(leftBarBtnClicked)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    self.navigationItem.titleView = titleLabel;
//    self.titleArray = @[@"全部",@"已接单",@"已装货",@"已卸货",@"已核算",@"已完成"];
//    self.titles = @[@"0",@"1",@"2",@"3",@"4",@"5"];
    self.titleArray = @[@"全部",@"已接单",@"已装货",@"已卸货",@"异常运单",@"未核算",@"待审核",@"已核算",@"已完成"];
    self.titles = @[@"0",@"1",@"2",@"3",@"10",@"7",@"6",@"4",@"5"];
    
    self.arrBadgeNums = [[NSMutableArray alloc]init];
    for (int i = 0; i < self.titles.count; i++) {
        [self.arrBadgeNums addObject:@"0"];
      }
    
     self.menuViewStyle = WMMenuViewStyleLine;
     self.titleSizeSelected = 16;
     self.titleSizeNormal = 14;
     //
     self.progressViewIsNaughty = YES;
     self.progressWidth = 30;
     self.progressHeight = 2;
     self.titleColorNormal = [UIColor cjMainTextColor];
     self.titleColorSelected = [UIColor cjMainColor];
     //   self.preloadPolicy = WMPageControllerPreloadPolicyNever;
     //         self.menuBGColor = [UIColor whiteColor];
     self.showOnNavigationBar = NO;
    
     [super viewDidLoad];

    CJRegisterNotify(CJOrderDetailListBadgeNum, @selector(getOrderListTitleBadgeNum:));
    if ([CJRegular getIsStrEmpty:self.strCode]) {
        //两个按钮的父类view
        UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, kNavBarHeight)];
        [rightButtonView addSubview:self.btnSearch];
        [rightButtonView addSubview:self.btnOrderList];
        //把右侧的两个按钮添加到rightBarButtonItem
        UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
        self.navigationItem.rightBarButtonItem = rightCunstomButtonView;
   
    }else{
       
        //两个按钮的父类view
        UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, kNavBarHeight)];
        [rightButtonView addSubview:self.btnSearch];
        //把右侧的两个按钮添加到rightBarButtonItem
        UIBarButtonItem *rightCunstomButtonView = [[UIBarButtonItem alloc] initWithCustomView:rightButtonView];
        self.navigationItem.rightBarButtonItem = rightCunstomButtonView;
    }
    
}

- (void)jumpToOrderList{
//    SourceViewController *vc = [[SourceViewController alloc]init];
//    [self pushVc:vc];
    
    CJSendNotify(@"clearSourcesBageNum", @"");
    
    if (self.navigationController == nil) return ;
    [self.navigationController popViewControllerAnimated:YES];
}


/// 货源单搜索
- (void)searchOrderListAction{
    
//    NSDictionary *dicPra = @{
//        @"mainOrderNumber":self.tfMainOrderNumber.text,
//        @"placeOfShipment":self.tfPlaceOfShipment.text,
//        @"placeOfReceipt":self.tfPlaceOfReceipt.text,
//        @"coalTypeName":self.tfCoalTypeName.text,
//        @"orderCode":self.tfOrderCode.text,
//        @"driverInfo":self.tfDriverInfo.text,
//        @"vehicleLicense":self.tfVehicleLicense.text,
//        @"bageNums":@(bageNums)
//    };
//    
    
    WS(ws);
    OrderListSearchVC *vc = [[OrderListSearchVC alloc]init];
    vc.dictInfo = self.dicSearch;
    vc.blockSerach = ^(id anyObject) {
        
        NSString *strBageNums = CJStringWithFormat(@"%@",anyObject[@"bageNums"]);
        if (strBageNums.intValue <= 0) {
            [ws.btnSearch clearBadge];
        }else{
            [ws.btnSearch showBadgeWithStyle:WBadgeStyleNumber value:strBageNums.intValue animationType:WBadgeAnimTypeNone];
            ws.btnSearch.badgeFrame = CGRectMake(40 - 13, 5, 13, 13);
        }
        
        ws.dicSearch = (NSDictionary *)anyObject;
        CJSendNotify(@"searchOrderdListAction", (NSDictionary *)anyObject);
        [ws reloadData];
    };
    [self pushVc:vc];
    
}


- (void)pushVc:(UIViewController *)vc {
    
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    if (self.navigationController == nil) return ;
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)leftBarBtnClicked{
     [self.navigationController popViewControllerAnimated:YES];
}

-(void)getOrderListTitleBadgeNum:(NSNotification *)notification{
//    DLog(@"=====%@",notification.object);
    
    NSArray *list = (NSArray *)notification.object;
    [self.arrBadgeNums removeAllObjects];
    [self.arrBadgeNums addObjectsFromArray:list];
//    DLog(@"------- %@",self.arrBadgeNums);
    
    for (int i = 0; i < self.arrBadgeNums.count; i++) {
        [self.menuView updateBadgeViewAtIndex:i];
    }
}
//1、返回分页数量，可以再这里设置标题栏各种样式
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return _titleArray.count;
}

//3、把标题与分页对应起来
- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return _titleArray[index];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    
    SourcesOrderListVC* vc  = [[SourcesOrderListVC alloc]init];
    vc.indexPage = self.titles[index];
//    vc.search_condition = self.titles[index];
    vc.strCode = self.strCode;
    vc.dicSearch = self.dicSearch;
    return vc;
    
}

/**
 *  角标 (例如消息提醒的小红点) 的数据源方法，在 WMPageController 中实现这个方法来为 menuView 提供一个 badgeView
    需要在返回的时候同时设置角标的 frame 属性，该 frame 为相对于 menuItem 的位置
 *
 *  @param index 角标的序号
 *
 *  @return 返回一个设置好 frame 的角标视图
 */
- (UIView *)menuView:(WMMenuView *)menu badgeViewAtIndex:(NSInteger)index{
    
    NSString *indexNum = self.arrBadgeNums[index];
    
   UIView *view = nil;
   if(indexNum.intValue != 0)
   {
       view = [[UIView alloc] initWithFrame:CGRectMake(self.menuItemWidth-15, 5, 15, 15)];
       view.layer.cornerRadius = 7.5;
       view.layer.masksToBounds = YES;
       view.backgroundColor = [UIColor cjRedColor];
       UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, view.width, view.height)];
       lab.textColor = [UIColor cjWhiteColor];
       if (indexNum.intValue > 99) {
           lab.text = @"99+";
       }else{
            lab.text = indexNum;
       }
       lab.textAlignment = NSTextAlignmentCenter;
       lab.adjustsFontSizeToFitWidth = YES;
       lab.font = [UIFont boldSystemFontOfSize:10];
       lab.backgroundColor = [UIColor cjClearColor];
       [view addSubview:lab];
   }
//    DLog(@"======= %ld",(long)index);
    return view;
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
    CGFloat width = [super menuView:menu widthForItemAtIndex:index];
    return width + 5;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    
    menuView.backgroundColor = [UIColor clearColor];
    //    CGFloat leftMargin = self.showOnNavigationBar ? 0 : 0;
//    CGFloat originY = self.showOnNavigationBar ? 0 : CGRectGetMaxY(self.navigationController.navigationBar.frame);
    return CGRectMake(0, kTopHeight, self.view.frame.size.width, 44);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, self.view.frame.size.height - originY);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark ----------lazy

- (CJButton *)btnSearch{
    if (!_btnSearch) {
        _btnSearch = CJButton.btnInit
        .btnFrame(CGRectMake(20, 0, 40, kNavBarHeight))
//        .btnText(@"运单")
//        .btnNormalTitleColor([UIColor cjMainTextColor])
//        .btnSelectTitleColor([UIColor cjMainTextColor])
        .btnTargetAction(self ,@selector(searchOrderListAction))
        .btnSelectImgName(@"zcj_icon_search_gray")
        .btnNormalImgName(@"zcj_icon_search_gray")
        ;
    }
    return _btnSearch;;
}

- (CJButton *)btnOrderList{
    if (!_btnOrderList) {
        _btnOrderList = CJButton.btnInit
        .btnFrame(CGRectMake(_btnSearch.right, _btnSearch.top, _btnSearch.width, _btnSearch.height))
        .btnText(@"货源")
        .btnNormalTitleColor([UIColor cjMainTextColor])
        .btnSelectTitleColor([UIColor cjMainTextColor])
        .btnTargetAction(self ,@selector(jumpToOrderList))
        ;
    }
    return _btnOrderList;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
